'use strict';
/**
 * 员工表
 */
module.exports = app => {
  const { STRING, UUID, NOW, DATE, UUIDV4 } = app.Sequelize;
  const OStaff = app.model.define('OStaff', {
    id: {
      type: UUID,
      primaryKey: true,
      allowNull: false,
      defaultValue: UUIDV4,
      comment: '用户id'
    },
    userName: {
      type: STRING(36),
      allowNull: false,
      field: 'user_name',
      comment: '账号'
    },
    wxId: {
      type: STRING(36),
      field: 'wx_id',
      comment: '微信id'
    },
    departmentId: {
      type: STRING(36),
      field: 'department_id',
      comment: '部门id'
    },
    password: {
      type: STRING(255),
      field: 'password',
      comment: '密码'
    },
    wxAvatar: {
      type: STRING(255),
      allowNull: true,
      field: 'wx_avatar',
      comment: '微信头像'
    },
    wxName: {
      type: STRING(255),
      allowNull: true,
      field: 'wx_name',
      comment: '微信昵称'
    },
    remark: {
      type: STRING(255),
      allowNull: true,
      field: 'remark',
      comment: '备注'
    },
    createDate: {
      type: DATE,
      defaultValue: NOW,
      field: 'create_date',
      comment: '创建时间'
    },
    updateDate: {
      type: DATE,
      defaultValue: NOW,
      field: 'update_date',
      comment: '更新时间'
    }
  }, {
    // 去除createAt，updateAt
    timestamps: false,
    // 实例对应的表名
    tableName: 'o_staff',
    // 将createdAt对应到数据库的created_at字段
    createdAt: 'create_date',
    // 将updatedAt对应到数据库的updated_at字段
    updatedAt: 'updated_date'
  });

  OStaff.associate = function () {
    app.model.Organization.Staff.belongsToMany(app.model.Organization.Role, {
      through: app.model.Organization.StaffRole,
      foreignKey: 'staffId',
      otherKey: 'roleId'
    })

    app.model.Organization.Staff.belongsTo(app.model.Organization.Departments, {
      foreignKey: 'departmentId',
      targetKey: 'id'
    })

  }

  return OStaff;
};